﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using BartonExtension;

using Cache.Model;
using Cache;

namespace UnitTest
{
    public class TestCacheClass
    {
        public string ID { get; set; }
        public string foo { get; set; }
    }
    public class CacheTest
    {
        public void Run()
        {
            Console.WriteLine("Caching test ....");

            List<TestCacheClass> classes = new List<TestCacheClass>();
            for (int i = 0; i < 10;i++)
            {
                TestCacheClass obj = new TestCacheClass()
                {
                    ID = IDGenerator.GenerateID().ToString(),
                    foo = "test" + i
                };
                classes.Add(obj);
            }

            Console.WriteLine("Flushing..");
            CacheManager.instance.CleanPool<TestCacheClass>();
            CachingResult<List<TestCacheClass>> result = CacheManager.instance.doCacheItemsToPool<TestCacheClass>(classes);
            if(result.isSuccessCached)
            {
                Console.WriteLine("Cached success");
                Console.WriteLine("Reading..");
                List<TestCacheClass> get = CacheManager.instance.GetItemsFromPool<TestCacheClass>();
                get.ForEach(g =>
                    {
                        Console.WriteLine("Cached data : " + g.foo);
                    });
                Console.WriteLine("Delete item");
                CacheManager.instance.DeleteItemFromPool<TestCacheClass>(get.First());
                Console.WriteLine("Reading..");
                get = CacheManager.instance.GetItemsFromPool<TestCacheClass>();
                Console.WriteLine("Storage count : " + get.Count);
            }
            else
            {
                Console.WriteLine("Cached fail.");
            }
            Console.ReadKey();
        }
    }
}
